<?php 
class page_models_admin extends CI_Model{
	public function __construct(){
		$this->load->database();
	}
   public function create($page){

   		//creer produit
	   	$this->db->insert('page_article', $page);
	   	//prendre id auto increment
	   	$shop->id=$this->db->insert_id();
   		return 	$page->id;
   }
   private function valide_link($page_link){
	   	$query = $this->db->get_where('page_article', array('page_link' => $page_link));
	   	if($query->num_rows()>1){
	   		$random=substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyz"), 0, 4);
	   		return $page_link.'-'.$random;
	   	}
   		return $page_link;
   }
   public function edit($page){
   		$page->page_link=self::valide_link($page->page_link);
   		$this->db->update('page_article', $page, "id = {$page->id}");
   }
	public function get_infoPage($id){
		$query = $this->db->get_where('page_article', array('id' => $id), 1);
		if($query->num_rows()){
			$row=$query->result();
			return $row[0];
		}
		return false;
	}
   public function get_list_page($filter=array(),$sort=array(),$paging=array()){
   	$limit=$paging['number_affiche'];
   	$current=$paging['page_current']*$limit;
   	$where='';
   	$orderBy='';
   	$condition=array();
   	foreach ($filter as $nameFilter=>$valueFilter)
   	if(!empty($valueFilter)){
   		$where=' WHERE ';
   		break;
   	}
   	if(!empty($sort))
   	foreach ($sort as $sortName=>$valueSort)
   		$orderBy="ORDER BY $sortName $valueSort";
   
   	if($where){
   		if($filter['date']){
   			$condition[]="DATE_FORMAT(DATE( FROM_UNIXTIME(page_article.date_validate ) ),'%Y%m' )='{$filter['date']}'";
   		}
   		if(!empty($condition))$where.= implode(' AND ',$condition);
   	}
   	$sql="SELECT *
   	FROM page_article
   	$where
   	$orderBy
   	LIMIT $current,$limit
   	";
   	$get= $this->db->query($sql);
   	return $get->result();
   
   }
   public function get_month_year_create($limit=''){
   	if($limit) $limit=" LIMIT $limit";
   	$get= $this->db->query("SELECT DISTINCT DATE_FORMAT(DATE( FROM_UNIXTIME( date_create ) ),'%Y%m' )AS YearMonth,
   			DATE_FORMAT(DATE( FROM_UNIXTIME( date_create ) ),'%m' )AS month,
   			DATE_FORMAT(DATE( FROM_UNIXTIME( date_create ) ),'%Y' )AS year
   			from page_article  $limit");
   			return $get->result();
   
   }
   public function get_total_page($active=FALSE){
   	
   	$this->db->select("count(*) as numShop ")->from('page_article');
   	if($active) $this->db->where(array('page_active'=>1));
   	$get= $this->db->get();
   	$row=$get->result();
   	 
   	return $row[0]->numShop;
   }
}